﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Yunwei.Web.Models;
using Microsoft.AspNet.Identity;
using Yunwei.IBizLogic;
using Yunwei.BizLogic;
using Microsoft.AspNet.SignalR;
using Yunwei.Web.SinalR;

namespace Yunwei.Web.Controllers
{
    public class OrderController : Controller
    {
        IOrderService _orderService;
        IShoppingCartService _cartService;
        IFoodIngredientService _foodIngredientService;
        IRestaurantIngredientService _restaurantIngredientService;
        IDinnerTableService _dinnerTableService;
        IOrderFoodService _orderFoodService;
        public OrderController()
        {
            _orderService = new OrderService();
            _cartService = new ShoppingCartService();
            _foodIngredientService = new FoodIngredientService();
            _restaurantIngredientService = new RestaurantIngredientService();
            _dinnerTableService = new DinnerTableService();
            _orderFoodService = new OrderFoodService();
        }
        // GET: Order
        public ActionResult Index()
        {
            return View();
        }

        [System.Web.Mvc.Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Place(OrderViewModel ovm)
        {
            int user_Id = User.Identity.GetUserId<int>();
            ovm.User_Id = user_Id;
            ovm.TotalPrice = ovm.ShoppingCartsViewModel.Price;
            if(ModelState.IsValid)
            {
                var order = _orderService.Add(ovm.Order);
                if (order == null || order.Id < 0)
                {
                    return RedirectToAction("Error404", "Home");
                }
                else
                {
                    //修改座位状态
                    var dinnerTable = _dinnerTableService.Find(ovm.DinnerTableViewModel.Id);
                    dinnerTable.Status = 1;
                    _dinnerTableService.Update(dinnerTable);


                    //食材减掉
                    OrderViewModel orderViewModel = OrderViewModel.FromOrder(order);
                    //菜品循环
                    foreach (var food in orderViewModel.ShoppingCartsViewModel.ShoppingCarts)
                    {
                        _orderFoodService.Add(new Domain.Models.OrderFoodRel()
                        {
                            Count = food.Total,
                            Food_Id = food.Food_Id,
                            Order_Id = order.Id
                        });

                        var ingredients = _foodIngredientService.FindList(100, food.Food_Id, 0).ToList();
                        foreach (var ingredient in ingredients)
                        {
                            var restaurantIngredient = _restaurantIngredientService.FindByIngredient(ingredient.Ingredient_Id, food.Restaurant_Id);
                            if (restaurantIngredient != null)
                            {
                                restaurantIngredient.Stock -= ingredient.Amount * food.Total;
                                _restaurantIngredientService.Update(restaurantIngredient);
                            }
                        }
                    }


                    //清空购物车
                    var carts = _cartService.FindList(50, user_Id, 0, 0, 0).ToArray();
                    _cartService.Delete(carts);

                    //通知智能机器,改为餐馆确认后再通知
                    //var context = GlobalHost.ConnectionManager.GetHubContext<CommunicationHub>();
                    //context.Clients.All.SendMsg2Adapter("");
                }

                return View(OrderViewModel.FromOrder(order));
            }
            
            return View();
        }

        [System.Web.Mvc.Authorize]
        public ActionResult Detail(int id)
        {
            return View();
        }
    }
}